package com.fenghongzhang.day016;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class MusicUtil {
    public static final int SINGLE = 0;
    public static final int ORDER = 1;
    public static final int RANDOM = 2;


    //格式化时间
    public static String fmtDate(String s){
        int i = Integer.parseInt(s);
        if(i / 1000 % 60 >= 10){
            return i / 1000 / 60 +":"+ i / 1000 % 60;
        }else{
            return i / 1000 / 60 +":0"+ i / 1000 % 60;
        }
    }

    //返回sd的数据源
    public static List<Music> getData(Context context){
        List<Music> list = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor query = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if(query != null){
            while (query.moveToNext()){
                String title = query.getString(query.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String artist = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String duartion = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String data = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DATA));
                String size = query.getString(query.getColumnIndex(MediaStore.Audio.Media.SIZE));
                String ablumId = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

//                String pic = getPic(ablumId, context);
//                Log.i("TAG", "getPic: "+pic);
                //过滤短音频
                if (Integer.parseInt(duartion) >= 1000 * 60){
                    Music music = new Music(title,artist,duartion,data,size,ablumId);
                    list.add(music);
                }
            }
            query.close();
        }
        return list;
    }

    public static String getPic(String ablumId,Context context){
        Uri parse = Uri.parse("content://media/external/audio/albums/" + ablumId);
        ContentResolver contentResolver = context.getContentResolver();
        Cursor query = contentResolver.query(parse, null, null, null, null);
        String album_art = null;
        if(query != null){
            boolean b = query.moveToFirst();
            if (b){
                album_art = query.getString(query.getColumnIndex("album_art"));
            }
            query.close();
        }
        return album_art;

    }

}
